package com.itheima.controller.backend.security;

import com.itheima.Service.CheckGroupService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/checkgroup")
@RestController("backendCheckGroupController")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,String checkitemIds){
        log.info("新增检查组:{},{}",checkGroup,checkitemIds);
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true,"添加成功");
    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result del(String id) throws Exception {
        log.info("根据id删除检查组:{}",id);
        checkGroupService.del(id);
        return new Result(true,"删除成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result getById(String id){
        log.info("根据id查询:{}",id);
        CheckGroup checkGroup= checkGroupService.findById(id);
        return new Result(true,"查询成功",checkGroup);
    }

    /**
     * 修改检查组
     * @param
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestBody CheckGroup checkGroup,String checkitemIds) throws Exception {
        log.info("修改检查组:{},{}",checkGroup,checkitemIds);
        checkGroupService.update(checkGroup,checkitemIds);
        return new Result(true,"修改成功");
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("/findPage")
    public PageResult PageQuery(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}",queryPageBean);
        PageResult pageResult= checkGroupService.PageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查组");
        List<CheckGroup> checkGroupList=checkGroupService.getAll();
        return new Result(true,"查询所有检查项",checkGroupList);
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(String setmealId){
        log.info("根据检查组id查询关联的检查项id:{}",setmealId);
        List<Integer> CheckGroupIds= checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true,"查询检查项成功",CheckGroupIds);
    }
}
